package com.epoint.juc.callable;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class SemaphoreDemo {

    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(2);

        for(int i = 0; i < 6; i++){
            new Thread(()->{
                try {
                    semaphore.acquire();
                    System.out.println("当前车位：" + Thread.currentThread().getName() + "，进去");
                    TimeUnit.SECONDS.sleep(2);
                    semaphore.release();
                    System.out.println("当前车位：" + Thread.currentThread().getName() + "，出去");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }, i + "").start();

        }



    }

}
